#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, k;
int a[N];
ll dp[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    rep(i, 1, n) cin >> a[i];
    priority_queue<int, vector<int>, greater<int>> pq;
    int cnt = 0;
    ll sum = 0;
    per(i, n - k, 1) {
      int p = (n + 1 - i) / (k + 1);
      if (p > cnt) {
        cnt = p;
        pq.push(a[i]);
        sum += a[i];
      } else {
        if (pq.empty()) continue;
        int mn = pq.top();
        if (a[i] > mn) {
          pq.pop();
          pq.push(a[i]);
          sum += a[i] - mn;
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}